<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/9
 * Time: 14:19
 */

namespace app\admin\controller;

use think\Request;
class Config extends common
{

    /**
     * 重庆时时彩
     */
    public function ssc(){
        ignore_user_abort();//关闭浏览器仍然执行
        set_time_limit(0);//让程序一直执行下去
        do{
            //跳出定时判定
            $info= db('config')->where('id',1)->find();
            $sscOpen = $info['code_open'];
            $sscTime = $info['code_time'];
            if($sscOpen != 1){
                die;
            }
            // 提前一天插入120条第二天要开奖的数据
            $expect = date('Ymd',time()+3600*24) . '%';
            if(db('ssc')->where('expect','like',$expect)->count() == 0){
                for ($i = 119; $i>=0 ;$i--){
                    if($i<10){
                        $i = '0'.$i;
                    }
                    if ($i >= 100){
                        $data['expect'] = date('Ymd',time()+3600*24).$i;
                    }else{
                        $data['expect'] = date('Ymd',time()+3600*24) . '0'.$i;
                    }
                    $data['expect'] ++;
                    $data['opencode'] = '--';
                    $data['opentime'] = '--';
                    $data['opentimestamp'] = '--';
                    $data['handcode'] = '--';
                    $data['state'] = 2;
                    db('ssc')->insert($data);
                }
            }
            //采集接口彩票今天数据 对比入库
            $date = date("Y-m-d");
            $url = "http://ho.apiplus.net/daily.do?token=t2a3a08213f3df40ck&code=cqssc&format=json&date=$date";
            $data1 = file_get_contents($url);
            $data1 = json_decode($data1,true);
            $data1 = $data1['data'];
            foreach ($data1 as $v){
                $find = db('ssc')->where('expect',$v['expect'])->find();
                //如果有这条数据
                if($find){
                    //其数据开奖结果和接口不同就更新入库
                    if($find['opencode'] != $v['opencode']){
                        $v['state'] = 1;
                        db('ssc')->where('expect',$v['expect'])->update($v);
                    }
                }else{//如果没有今天提前插入的数据就插入
                    $expect = date('Ymd') . '%';
                    if(db('ssc')->where('expect','like',$expect)->count() == 0){
                        for ($i = 119; $i>=0 ;$i--){
                            if($i<10){
                                $i = '0'.$i;
                            }
                            if ($i >= 100){
                                $data2['expect'] = date('Ymd').$i;
                            }else{
                                $data2['expect'] = date('Ymd') . '0'.$i;
                            }
                            $data2['expect'] ++;
                            $data2['opencode'] = '--';
                            $data2['opentime'] = '--';
                            $data2['opentimestamp'] = '--';
                            $data2['handcode'] = '--';
                            $data2['state'] = 2;
                            db('ssc')->insert($data2);
                        }
                    }
                }
            }
            sleep($sscTime);//等待时间，进行下一次操作。
        }while(true);
    }


    //系统设置
    public function configSet(){
        if (Request::instance()->isPost()){
            $data['web_open'] = input('post.web_open');
            $data['web_text'] = input('post.web_text');
            $data['total_bet'] = input('post.total_bet');
            $data['agent_bet'] = input('post.agent_bet');
            $data['charge'] = input('post.charge');
            $data['code_style'] = input('post.code_style');
            $data['return_max'] = input('post.return_max');
            $data['return_min'] = input('post.return_min');
            $data['charge_min'] = input('post.charge_min');
            $data['charge_max'] = input('post.charge_max');
            $data['drawmoney_min'] = input('post.drawmoney_min');
            $data['drawmoney_max'] = input('post.drawmoney_max');
            $data['clear_user_money'] = input('post.clear_user_money');
            $data['clear_user_time'] = input('post.clear_user_time');
            $data['code_money'] = input('post.code_money');
            $data['odds'] = input('post.odds');
            if(db('webconfig')->where('id',1)->update($data)){
                $this->success('更新成功!');
            }else{
                $this->error('更新失败!');
            }

        }else{
            $data = db('webconfig')->where('id',1)->find();
            $this->assign('data',$data);
            return $this->fetch();
        }
    }
    //清理用户
    public function clearUser(){
        $money = input('get.money/d');
        $time = input('get.time/d');
        if(trim($money) != '' && trim($time) != ''){
            $time = time() - $time*3600*24;//算出时间戳
            $data = db('user')->where('money','<',$money)->select();
            foreach ($data as $v){
                if(strtotime($v['last_time'])<$time){
                    db('user')->where('id',$v['id'])->update(['state'=>2]);
                }
            }
            $this->success('符合条件用户已全部冻结!');
        }else{
            $this->error('参数错误!');
        }
    }


    //采集器列表
    public function codeSet(){
        $data= db('config')->select();
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function codeEdit(){
        if(Request::instance()->isPost()){
            $id= input('post.id/d');
            $time = input('post.time');
            $open = input('post.open');
            if(trim($id) != '' && trim($time) != '' && trim($open) != ''){
                $bool = db('config')->where('id',$id)->update(['code_time'=>$time,'code_open'=>$open]);
                if($bool){
                    return json_encode(['msg'=>1,'data'=>'更新成功,刷新页面显示最新数据!']);
                }else{
                    return json_encode(['msg'=>0,'data'=>'更新失败!']);
                }
            }else{
                return json_encode(['msg'=>0,'data'=>'提交数据为空!']);
            }
        }else{
            $id = input('get.id/d');
            $data =  db('config')->where('id',$id)->find();
            $this->assign('data',$data);
            return $this->fetch();
        }
    }
}